package com.gs.synedocket.fragment.docket;

import com.gs.synedocket.R;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.constant.EventCode;
import com.gs.synedocket.control.DocketSummaryView;
import com.gs.synedocket.control.SignPhotoView;
import com.gs.synedocket.db.bean.Event;
import com.gs.synedocket.db.dto.DocketDto;
import com.gs.synedocket.db.dto.DocketSiteDto;
import com.gs.synedocket.db.dto.ShortDto;
import com.gs.synedocket.dialog.LoadingDialogUtils;
import com.gs.synedocket.dialog.ToastDialogUtils;
import com.gs.synedocket.fragment.base.BaseFragment;
import com.gs.synedocket.presenter.DocketLogDetailPresenter;
import com.gs.synedocket.util.DateUtil;
import com.gs.synedocket.util.DocketUtil;
import com.gs.synedocket.view.IDocketLogDetailView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class DocketLogDetailFragment extends BaseFragment<IDocketLogDetailView, DocketLogDetailPresenter> implements IDocketLogDetailView {
    private DocketSummaryView viewBuyerName, viewSiteLoc, viewContactPerson, viewTel, viewVehicleNo,
            viewPlant, viewConcreteMix, viewSlump, viewDate, viewCementBrand, viewAdmixtures,
            viewBuyerCode, viewSiteCode, viewCustomerPo, viewMaxAgg;
    private SignPhotoView viewQC, viewOK;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_docket_log_detail;
    }

    @Override
    protected DocketLogDetailPresenter createPresenter() {
        return new DocketLogDetailPresenter();
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        viewBuyerName = fd(R.id.view_buyer_name);
        viewSiteLoc = fd(R.id.view_site_loc);
        viewContactPerson = fd(R.id.view_contact_person);
        viewTel = fd(R.id.view_tel);
        viewCustomerPo = fd(R.id.view_customer_po);
        viewVehicleNo = fd(R.id.view_vehicle_no);
        viewPlant = fd(R.id.view_plant);
        viewConcreteMix = fd(R.id.view_concrete_mix);
        viewSlump = fd(R.id.view_slump);
        viewDate = fd(R.id.view_date);
        viewCementBrand = fd(R.id.view_cement_brand);
        viewMaxAgg = fd(R.id.view_max_agg);
        viewAdmixtures = fd(R.id.view_admixtures);
        viewBuyerCode = fd(R.id.view_buyer_code);
        viewSiteCode = fd(R.id.view_site_code);
        viewQC = fd(R.id.view_signature1);
        viewOK = fd(R.id.view_signature2);
    }

    @Override
    protected void initData() {
        mPresenter.getData(DocketUtil.getInstance().getDocketId(), true);
    }

    @Override
    public void onGetDataSuccess(ApiResultData<DocketDto> apiResultData, boolean isApi) {
        if (apiResultData.getStatus() != 2000) {
            if (isApi) { // 接口查询失败时查询本地数据库
                mPresenter.getData(DocketUtil.getInstance().getDocketId(), false);
            } else {
                ToastDialogUtils.getInstance().showInfoMessage(context, apiResultData.getMessage());
            }
            return;
        }
        DocketDto data = apiResultData.getData();
        if (data == null || data.getId() == null) {
            ToastDialogUtils.getInstance().showInfoMessage(context, R.string.empty_docket);
            refreshView();
            return;
        }
        viewContactPerson.setContent(data.getContactPerson() == null ? "" : data.getContactPerson());
        viewTel.setContent(data.getContactTel() == null ? "" : data.getContactTel());
        if (data.getCustomer() != null) {
            ShortDto customer = data.getCustomer();
            viewBuyerName.setContent(customer.getName() == null ? "" : customer.getName());
            viewBuyerCode.setContent(customer.getCode());
        } else {
            viewBuyerName.setContent("");
            viewBuyerCode.setContent("");
        }
        if (data.getSite() != null) {
            DocketSiteDto site = data.getSite();
            String siteName = site.getName();
            String extraAddress = data.getExtraAddress();
            String name = (siteName == null ? "" : siteName) + (extraAddress == null ? "" : " " + extraAddress);
            viewSiteLoc.setContent(name);
            DocketUtil.getInstance().setDocketSite(name);
            viewSiteCode.setContent(site.getCode() == null ? "" : site.getCode());
        } else {
            viewSiteLoc.setContent("");
            viewSiteCode.setContent("");
        }
        if (data.getProduct() != null) {
            viewConcreteMix.setContent(data.getProduct().getName() == null ? "" : data.getProduct().getName());
        } else {
            viewConcreteMix.setContent("");
        }
        if (data.getCement() != null) {
            viewCementBrand.setContent(data.getCement().getName() == null ? "" : data.getCement().getName());
        } else {
            viewCementBrand.setContent("");
        }
        if (data.getAdmixtures() != null && !data.getAdmixtures().isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (ShortDto bean : data.getAdmixtures()) {
                sb.append(",").append(bean.getName() == null ? "" : bean.getName());
            }
            viewAdmixtures.setContent(sb.substring(1));
        } else {
            viewAdmixtures.setContent("");
        }
        if (data.getPlant() != null) {
            viewPlant.setContent(data.getPlant().getName() == null ? "" : data.getPlant().getName());
        } else {
            viewPlant.setContent("");
        }
        viewQC.setImageUrl(data.getQcPhoto());
        viewOK.setImageUrl(data.getAddcPhoto());
        viewMaxAgg.setContent(data.getNominalMaxAgg() == null ? "" : data.getNominalMaxAgg() + "MM");
        viewSlump.setContent(data.getSlump() == null ? "" : data.getSlump() + "");
        viewCustomerPo.setContent(data.getCustPo() == null ? "" : data.getCustPo());
        viewVehicleNo.setContent(data.getTruckNo() == null ? "" : data.getTruckNo());
        viewDate.setContent(DateUtil.DateTimeToStr(data.getDocketAt()));
        DocketUtil.getInstance().setDocketId(data.getId());
        DocketUtil.getInstance().setTruckState(data.getTruckState());
        DocketUtil.getInstance().setDriverState(data.getState());
        DocketUtil.getInstance().setQcState(data.getQcState());
        DocketUtil.getInstance().setTruckNo(data.getTruckNo());
        DocketUtil.getInstance().setDocketNo(data.getDocketNo());
        DocketUtil.getInstance().setQrCodeAuth(data.getAuthorityQRCode() == null ? "" : data.getAuthorityQRCode());
        DocketUtil.getInstance().setQrCodeCus(data.getCustQRCode() == null ? "" : data.getCustQRCode());
        DocketUtil.getInstance().setQrCodeKWah(data.getkWahQRCode() == null ? "" : data.getkWahQRCode());
        DocketUtil.getInstance().setAccRep(data.getAccRep());
        DocketUtil.getInstance().setPhotos(data.getDocketPhotos());
        DocketUtil.getInstance().setDriverRemark(data.getDriverRemark());
        DocketUtil.getInstance().setAdditional(data.getAdditional());
        DocketUtil.getInstance().setArriveSiteAt(data.getArriveSiteAt());
        DocketUtil.getInstance().setArriveSiteGps(data.getArriveSiteGps() == null ? "" : data.getArriveSiteGps());
        DocketUtil.getInstance().setStartDisAt(data.getStartDisAt());
        DocketUtil.getInstance().setStartDisGps(data.getStartDisGps() == null ? "" : data.getStartDisGps());
        DocketUtil.getInstance().setEndDisAt(data.getEndDisAt());
        DocketUtil.getInstance().setEndDisGps(data.getEndDisGps() == null ? "" : data.getEndDisGps());
        DocketUtil.getInstance().setLeaveSiteAt(data.getLeaveSiteAt());
        DocketUtil.getInstance().setLeaveSiteGps(data.getLeaveSiteGps() == null ? "" : data.getLeaveSiteGps());
        DocketUtil.getInstance().setArrivePlantAt(data.getArrivePlantAt());
        DocketUtil.getInstance().setArrivePlantGps(data.getArrivePlantGps() == null ? "" : data.getArrivePlantGps());
        EventBus.getDefault().post(new Event(EventCode.EVENT_DONE_ON_HAND, false));
    }

    private void refreshView() {
        DocketUtil.getInstance().refresh();
        viewBuyerName.setContent("");
        viewContactPerson.setContent("");
        viewTel.setContent("");
        viewBuyerCode.setContent("");
        viewSiteLoc.setContent("");
        viewSiteCode.setContent("");
        viewConcreteMix.setContent("");
        viewSlump.setContent("");
        viewCementBrand.setContent("");
        viewMaxAgg.setContent("");
        viewAdmixtures.setContent("");
        viewPlant.setContent("");
        viewVehicleNo.setContent("");
        viewDate.setContent("");
        EventBus.getDefault().post(new Event(EventCode.EVENT_DONE_ON_HAND, false));
    }

    @Override
    public void onGetDataFail(Exception e) {
        refreshView();
        ToastDialogUtils.getInstance().showInfoMessage(context, R.string.network_error);
    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(context);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        int what = event.getWhat();
        if (what == EventCode.EVENT_LOG_DETAIL) {
            initData();
        }
    }
}
